package Thread.Thread_Security;

import java.util.Scanner;

public class Demo28 {
    public static Object locker = new Object();
    public static void main(String[] args) {
        Thread t1=new Thread(()->{
            System.out.println("t1 wait之前");
            synchronized (locker){
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1 wait之后");
            }
        });
        Thread t2=new Thread(()->{
            System.out.println("t2 wait之前");
            synchronized (locker){
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t2 wait之后");
            }
        });
        Thread t3=new Thread(()->{
            System.out.println("t3 wait之前");
            synchronized (locker){
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t3 wait之后");
            }
        });
        Thread t4=new Thread(()->{
            System.out.println("t4 notifyAll之前");
            Scanner sc=new Scanner(System.in);
            sc.next();
            synchronized (locker){
//                locker.notify();
                locker.notifyAll();//唤醒所有等待线程
                //一个一个唤醒的话，程序执行过程是比较有序的，如果一次唤醒所有等待线程，可能会不可控
            }
            //notify即使在notify多次之后，有的没有收到回应也不会出现什么异常或者副作用的
            System.out.println("t4 notifyAll之后");
        });
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
